﻿using DefaultDspPlugins.UI.Common.Modulizer.Engine.GUIv2.Controls;
using DefaultDspPlugins.UI.Common.Modulizer.Engine.GUIv2.FrontEndComponents;
using SharpDepend.Datatypes;
using System;
using ZquenceStudio3Host.Graphics.Components;
using ZquenceStudio3Host.Graphics.GUI;

namespace DefaultDspPlugins.UI.Common.Modulizer.Engine.GUIv2.Editors
{
    class ModuleUIToolsPanel : Panel
    {
        private int mPos;
        private Button mAddControl;
        private Button mRemoveControl;
        private Listbox mComponentListbox;
        private BaseObject mEditablePanel;
        private ModuleUIPanel mOriginalModuleUIpanel;
        private Type mCurrentComponentToAdd = typeof(Panel);

        public BaseObject EditablePanel
        {
            get { return mEditablePanel; }
            internal set
            {
                if (value is ModuleUIPanel)
                {
                    mOriginalModuleUIpanel = value as ModuleUIPanel;
                }

                mEditablePanel = value;
                SaveStructure();
            }
        }

        public event Action PanelUpdated;

        public ModuleUIToolsPanel(BasicGuiManager engineCore) : base(engineCore)
        {
            mAddControl = new Button(engineCore);
            mAddControl.Label.Text = "Add";
            mAddControl.OnClick += MAddControl_OnClick;
            AddButton(mAddControl);

            mRemoveControl = new Button(engineCore);
            mRemoveControl.OnClick += MDRemoveControl_OnClick;
            mRemoveControl.Label.Text = "Remove";
            AddButton(mRemoveControl);

            mComponentListbox = new Listbox(engineCore);
            mComponentListbox.BackgroundColor = Color.Black;
            mComponentListbox.OnSelectedListboxItem += MComponentListbox_OnSelectedListboxItem;
            mComponentListbox.DimensionInProcent = true;
            mComponentListbox.Width = 100;
            mComponentListbox.Height = 50;
            mComponentListbox.Y = 50;
            AddDisplayComponent(typeof(Panel));
            AddDisplayComponent(typeof(DialButton));
            AddDisplayComponent(typeof(ParameterDialButton));
            AddDisplayComponent(typeof(HorizontalSlider));
            AddDisplayComponent(typeof(PianoOctave));
            AddDisplayComponent(typeof(Dial));
            AddPart(mComponentListbox);
        }

        private void AddDisplayComponent(Type type)
        {
            mComponentListbox.AddItem(new ListboxItem(EngineCore, type.Name, type));
        }

        private void MComponentListbox_OnSelectedListboxItem(object sender, ListboxItem selectedItem)
        {
            mCurrentComponentToAdd = selectedItem.ItemObject as Type;


        }

        private void MAddControl_OnClick(object sender, double x, double y, int id)
        {
            if (mOriginalModuleUIpanel == null)
            {
                throw new Exception("Can not add a Panel when no ModuleUIPanel has been otiginaled loaded");
            }
            
            Panel newPanel = (Panel)Activator.CreateInstance(mCurrentComponentToAdd, EngineCore);
            newPanel.Tag = mOriginalModuleUIpanel;
            newPanel.DimensionInProcent = true;
            newPanel.X = 40;
            newPanel.Y = 40;
            newPanel.Width = 20;
            newPanel.Height = 20;

            EditablePanel.AddComponent(newPanel);

            StructureUpdated();
        }

        private void MDRemoveControl_OnClick(object sender, double x, double y, int id)
        {
            throw new NotImplementedException();
        }

        private void StructureUpdated()
        {
            SaveStructure();

            PanelUpdated?.Invoke();
        }

        private void SaveStructure()
        {
            mOriginalModuleUIpanel.SaveStructure();
        }

        private void AddButton(Button control)
        {
            control.DimensionInProcent = true;
            control.Width = 80;
            control.Height = 10;
            control.X = 10;
            control.Y = (10 + 1) * mPos++;
            AddPart(control);
        }
    }
}
